<!DOCTYPE html>
<html>

<head lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0" />
    <meta content="yes" name="apple-mobile-web-app-capable">
    <meta content="black" name="apple-mobile-web-app-status-bar-style">
    <meta content="telephone=no" name="format-detection">
    <title>收货地址管理</title>
    <link type="image/x-icon" rel="shortcut icon" href="../images/favicon.ico">
    <link rel="stylesheet" href="../res/mui/css/mui.min.css" />
    <link rel="stylesheet" href="../res/fontAwesome/css/font-awesome.min.css" />
    <link rel="stylesheet" href="../css/common.css" />
    <link rel="stylesheet" href="../css/user.css" />
    <style>
        select.sel{
        padding: 0 15px;
        box-sizing: border-box;
        color:#666;
    }
    </style>
</head>

<body>
    <div class="cz_layout bgg">
        <header class="cz_topBar">
            <a href="javascript:history.back();" class="icon_back fa fa-arrow-left"></a>
            <span class="title">地址信息</span>
        </header>
        <div class="cz_container">
            <form action="">

            </form>
        </div>
        <footer class="cz_tabs">
            <a class="tab_home fa fa-home" href="../index.html"><span>首页</span></a>
            <a class="tab_cate fa fa-bars" href="../cate.html"><span>分类</span></a>
            <a class="tab_cart fa fa-shopping-cart" href="../cart.html"><span>购物车</span></a>
            <a class="tab_user fa fa-user" href="../user/index.html"><span>会员中心</span></a>
        </footer>

    </div>
    <script src="../res/mui/js/mui.min.js"></script>
    <script src="/assets/vendors/jquery/jquery.min.js"></script>
    <script src="/assets/vendors/region/jquery.region.js"></script>
    <script src="/assets/vendors/jquery.form/jquery.form.js"></script>
    <script src="/assets/vendors/layer/layer.js"></script>
    <script src="/assets/vendors/template-web.js"></script>
    <script type="text/template" id="tpl">
        {{each list value}}
            <div class="form_box">
                <!-- 隐藏域 获取id -->
                    <input name="id" type="hidden" value="{{value.id}}">
                <div class="mui-input-row">
                    <input name="recipients" type="text" placeholder="收货人" value="{{value.recipients}}">
                </div>
                <div class="mui-input-row">
                    <input name="postcode" type="text" placeholder="邮编" value="{{value.postCode}}">
                </div>
                <div class="mui-input-row">
                    <div class="box">
                        <input type="text" name="address" value="{{value.address}}">
                        <!-- <select name="" class="sel" id="p" data-id="">
                            <option value=""> 请选择省份</option> -->
                            <!-- <optgroup label="请选择省份"></optgroup> -->
                        <!-- </select>
                        <select name="" class="sel" id="c" data-id="">
                            <option value=""> 请选择城市</option>
                        </select>
                        <select name="" class="sel" id="d" data-id="">
                            <option value=""> 请选择县区</option>
                        </select> -->
                    </div>
                </div>
                <div class="mui-input-row">
                    <textarea name="addressDetail" id="textarea" rows="5" placeholder="详细地址">{{value.addressDetail}}</textarea>
                </div>
                <button class="mui-btn mui-btn-primary btn_login">确认</button>
            </div>
        {{/each}}
    </script>
    <script>
        //获取修改页面传过来的id
        var id = location.search.slice(1);
        //准备接收处理后的参数
        var qs = {};
        //按照 & 把地址进行拆分为数组
        var arr = id.split('&');
        // 遍历数组  以 = 拆分
        arr.forEach(function (val) {
            //tmp 是数组
            var tmp = val.split('=');
            qs[tmp[0]] = tmp[1];
        })
        console.log(qs); //{id:10}

        $.ajax({

            url: "/api/address/queryAddress",
            success: function (msg) {
                //判断用户是否登录
                if (msg.error) return location.href = '/user/login.html?url=' + location.href;
                var lists = msg.filter(function (val) {
                    return val.id == qs.id;
                })
                // console.log(lists);
                var strHtml = template('tpl', {
                    list: lists
                });
                // console.log(lists)
                $('form').html(strHtml);
            }
        });
        $('form').ajaxForm({
            url: '/api/address/updateAddress',
            type: 'post',
            success: function (msg) {
                if (msg.error) return location.href = '/user/login.html?url=' + location.href;
                layer.msg('修改成功', {
                    shift: 5,
                    time: 500
                }, function () {
                    location.href = '/user/address.html'
                })
            }
        });
    </script>
</body>

</html>